package AI.screens
{
	import com.cheezeworld.AI.AISettings;
	import com.cheezeworld.AI.Behaviors.*;
	import com.cheezeworld.AI.Wall;
	import com.cheezeworld.entity.*;
	import com.cheezeworld.math.MathUtils;
	import com.cheezeworld.math.Vector2D;
	import com.cheezeworld.rendering.MovingEntityRenderer;
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.screens.Root;
	import com.cheezeworld.utils.Input;

	public class WallAvoidanceDemo extends AIDemoScreen
	{
		public function WallAvoidanceDemo(a_parentScreen:IScreenItem=null)
		{
			super(a_parentScreen);
			
			screenLabel = "Wall Avoidance \n ( Buggy )";
			
			var i:int;
			
			// Create some walls
			var wall:Wall;
			m_walls = [];
			
			var points:Array = [];
			points.push( new Vector2D( 20, 20 ));
			points.push( new Vector2D( Root.WIDTH-20, 20 ));
			
			points.push( new Vector2D( Root.WIDTH-20, 20 ));
			points.push( new Vector2D( Root.WIDTH-20, Root.HEIGHT-20 ));
			
			points.push( new Vector2D( Root.WIDTH-20, Root.HEIGHT-20 ));
			points.push( new Vector2D( 20, Root.HEIGHT-20 ));
			
			points.push( new Vector2D( 20, Root.HEIGHT-20 ));
			points.push( new Vector2D( 20, 20 ));
			
			for( i=0; i<8; i++ )
			{
				wall = new Wall( points[ i++ ], points[ i ] );
				wall.draw( this.graphics, 0xFFFFFF );
				m_walls.push( wall );
			}
				
			
			// Create some boids
			
			var bigBoid:Boid;
			bigBoid = _factory.getEntity( "Boid1", _gameworld ) as Boid;
			bigBoid.steering.addBehavior( new Arrive( Input.instance.worldMousePos, AISettings.arriveFast ) );
			bigBoid.steering.addBehavior( new WallAvoidance( m_walls ) );
			bigBoid.newPos.Set( 250, 250 );	
			_factory.getRenderer( bigBoid, MovingEntityRenderer );
			
			var boid:Boid;
			for( i = 0; i < 30; i++ )
			{
				boid = _factory.getEntity( "Boid1", _gameworld ) as Boid;
				boid.newPos.Set( MathUtils.rand( 0, Root.WIDTH ), MathUtils.rand( 0, Root.HEIGHT ));
				boid.steering.addBehavior( new Alignment() );
				boid.steering.addBehavior( new Cohesion() );
				boid.steering.addBehavior( new Seperation() );
				boid.steering.addBehavior( new WallAvoidance( m_walls ));
				boid.steering.addBehavior( new Evade( bigBoid, 75*75 ));
				boid.scale = .5;
			}
			
		}	
		
		/* 
		public override function update(a_timePassed:int):void
		{
			super.update(a_timePassed);
			
			if( Input.instance.isMousePressed )
			{
				setPoint( Input.instance.worldMousePos );
			}
		}
		
		private function setPoint( a_point:Vector2D ) : void
		{
			if( m_A == null )
			{
				m_A = a_point.copy();
			}
			else
			{
				var wall:Wall = new Wall( m_A, a_point.copy() );
				wall.draw( this.graphics, 0xFFFFFF );
				m_walls.push( wall );
				m_A = null;
			}
		}
		 */
		private var m_walls:Array;
		private var m_A:Vector2D;
		
	}
}